package com.training.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.base.BaseActivity;
import com.training.R;
import com.training.bean.Feedback;
import com.training.databinding.ActivityFeedbackBinding;
import com.training.net.HttpResponse;
import com.training.bean.User;

import com.training.net.NetCallback;
import com.training.util.ToastyUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FeedbackActivity extends BaseActivity<ActivityFeedbackBinding> {

    @Override
    public ActivityFeedbackBinding binding() {
        return ActivityFeedbackBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onCreated(Bundle savedInstanceState) {
        mBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 设置提交按钮点击事件
        mBinding.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitFeedback();
            }
        });
    }

    @Override
    public void loadData() {

    }

    @Override
    public void refreshUI() {

    }

    /**
     * 提交意见反馈
     */
    private void submitFeedback() {
        String content = mBinding.etFeedbackContent.getText().toString().trim();

        // 检查输入是否为空
        if (content.isEmpty()) {
            ToastyUtil.showInfo(context, "请输入反馈内容", Toast.LENGTH_SHORT, true);
            return;
        }

        // 检查输入长度
        if (content.length() > 200) {
            ToastyUtil.showInfo(context, "反馈内容不能超过200个字符", Toast.LENGTH_SHORT, true);
            return;
        }

        // 禁用提交按钮防止重复提交
        mBinding.btnSubmit.setEnabled(false);
        mBinding.btnSubmit.setText("提交中...");

        // 调用接口提交反馈
        dc.addFeedback(content, new NetCallback<HttpResponse<Feedback>>() {
            @Override
            public void onError(int code, String message) {
                ToastyUtil.showError(context, message, Toast.LENGTH_SHORT, true);
            }

            @Override
            public void onSuccess(HttpResponse<Feedback> response) {
                ToastyUtil.showSuccess(context, "反馈提交成功，感谢您的意见！", Toast.LENGTH_SHORT, true);
                // 清空输入框
                mBinding.etFeedbackContent.setText("");
            }

            @Override
            public void onFinally() {
                mBinding.btnSubmit.setEnabled(true);
                mBinding.btnSubmit.setText(R.string.submit);
            }
        });
    }
}